package org.example.facade;

import org.example.chain.FaultHandlingHandler;
import org.example.factory.AbstractFarmMachineFactory;
import org.example.factory.FarmMachineFactory;
import org.example.observer.EnvironmentData;
import org.example.observer.EnvironmentMonitor;
import org.example.model.FarmMachine;
import org.example.strategy.PathPlanningContext;
import org.example.command.CommandInvoker;
import org.example.command.ChangePathCommand;
import org.example.command.StartTaskCommand;
import org.example.chain.SafetyData;
import org.example.chain.SafetyHandler;
import org.example.strategy.NormalPathStrategy;

public class FarmSystemFacade {
    private AbstractFarmMachineFactory machineFactory;
    private EnvironmentMonitor environmentMonitor;
    private PathPlanningContext pathPlanningContext;
    private CommandInvoker commandInvoker;
    private SafetyHandler safetyChain;

    public FarmSystemFacade() {
        // 初始化工厂
        machineFactory = new FarmMachineFactory();

        // 初始化环境监控器
        environmentMonitor = new EnvironmentMonitor();

        // 初始化路径规划上下文
        pathPlanningContext = new PathPlanningContext();
        pathPlanningContext.setStrategy(new NormalPathStrategy());

        // 初始化命令调用器
        commandInvoker = new CommandInvoker();

        // 初始化安全处理链
        // 实际应用中应通过配置或工厂创建完整的链
        this.safetyChain = new FaultHandlingHandler();; // 此处需要根据实际情况设置
    }
    /**
     * 创建 machines
     * @param type
     * @param id
     * @return
     */
    public FarmMachine createFarmMachine(String type, String id) {
        FarmMachine machine = machineFactory.createFarmMachine(type, id);
        // 注册为观察者
        environmentMonitor.registerObserver(machine);
        // 设置默认路径规划策略
        machine.setPathPlanningContext(new PathPlanningContext());
        machine.getPathPlanningContext().setStrategy(new NormalPathStrategy());
        return machine;
    }
    /**
     * 更新环境数据
     * @param data
     */
    public void updateEnvironmentData(EnvironmentData data) {
        environmentMonitor.updateEnvironmentData(data);
    }
    /**
     * 计划机器的路径
     * @param machine
     */
    public void planPathForMachine(FarmMachine machine) {
        machine.getPathPlanningContext().planPath(machine);
    }
    /**
     * 为机器分配任务
     * @param machine
     */
    public void assignTaskToMachine(FarmMachine machine) {
        StartTaskCommand command = new StartTaskCommand(machine);
        commandInvoker.executeCommand(command);
    }
    /**
     * 处理安全事件
     * @param machine
     * @param event
     * @param data
     */
    public void handleSafetyEvent(FarmMachine machine, String event, SafetyData data) {
        if (safetyChain == null) {
            System.out.println("安全处理链未初始化");
            return;
        }
        safetyChain.handleSafetyEvent(machine, event, data);
    }
    /**
     * 撤销上一个命令
     */
    public void undoLastCommand() {
        commandInvoker.undoCommand();
    }
    /**
     * 重做上一个命令
     */
    public void redoLastCommand() {
        commandInvoker.redoCommand();
    }
    /**
     * 获取命令调用器
     * @return
     */
    public CommandInvoker getCommandInvoker() {
        return commandInvoker;
    }
}